#include <iostream>
#include <vector>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <chipmunk/chipmunk.h>
#include "MagnusEngine.h"
#include "Vectors.h"
#include "Entity.h"
#include "World.h"
#include "Camera.h"
#include "Event.h"
#include "Animation.h"

using namespace std;


class TestEngine : public me::MagnusEngine
{
    public:
        TestEngine(sf::VideoMode vMode, std::string windowTitle, sf::Uint32 style): me::MagnusEngine::MagnusEngine(vMode, windowTitle, style)
        {
            me::World* world = new me::World("Main", me::Vector2f(20, 20), me::Vector2f(32.0f, 32.0f), cpv(0, 10));
            m_worldManager.addWorld(world);
            m_worldManager.setCurrentWorld("Main");
            ballTexture = new sf::Texture();
            if (!ballTexture->loadFromFile("Data/Textures/Ball3.png"))
                cout << "Couldn't load texture\n";
        }
        ~TestEngine()
        {
            delete ballTexture;
        }

        void onClose()
        {
            cout << "Closing\n";
        }

        void onPreRender()
        {

        }

        void onRender()
        {
            me::World* world = m_worldManager.getCurrentWorld();
            vector<me::Entity*> entities = world->getEntities();
                for (unsigned int i=0 ; i < entities.size() ; i++)
                {
                    me::Vector2f screenPos = entities[i]->getWorldPosition();
                    screenPos.x *= world->getTileSize().x;
                    screenPos.y *= world->getTileSize().y;
                    entities[i]->setPosition(sf::Vector2f(screenPos.x, screenPos.y));
                    sf::Vector2f pos = entities[i]->getPosition();
                    cout << pos.x << ", " << pos.y << endl;
                    m_window.draw(*entities[i]);
                }
        }

        void onPostRender()
        {

        }

        void onKey(sf::Uint32 key, bool pressed, bool alt, bool control, bool shift, bool system)
        {

        }

        void onMouseClick(sf::Uint32 button, bool pressed, int x, int y)
        {
            if ( (button == sf::Mouse::Left) && (pressed) )
            {
                cout << "Creating new entity.\n";
                cpFloat moi = cpMomentForCircle(10, 1, 0, cpvzero);
                cpBody* body = cpBodyNew(10, moi);
                me::Vector2f sPos = me::Vector2f(x, y);
                me::Vector2f tSize = m_worldManager.getCurrentWorld()->getTileSize();
                me::PhysicalEntity* entity = new me::PhysicalEntity(body, me::Vector2f(sPos.x/tSize.x, sPos.y/tSize.y), me::Vector2f(0, 0), ballTexture);
                m_worldManager.getCurrentWorld()->addEntity(entity);
                cpShape* shape = cpCircleShapeNew(body, 1, cpvzero);
                entity->addShape(shape);
            }
        }

    private:
        sf::Texture* ballTexture;
};



int main()
{
    TestEngine engine(sf::VideoMode(640, 640), "Magnus Engine Testing", sf::Style::Close);
    while (engine.isActive())
    {
        engine.update();
    }

    return 0;
}

